package com.sxkiler.demo.zentao;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class ZentaoApiTestCase {

    public static final String api_address = "http://myhome.kingdee.com";
    /**
     * 2 授权接口 api/users/access_token   GET请求
     */
    String getsessionId()  throws Exception {
        String url =  api_address + "/zentao/api-getsessionid.json";
        System.out.println(url);

        //发送请求
        HttpResponse response = HttpUtil.createGet(url).execute();
        JSONObject obj =  JSON.parseObject(response.body());
        System.out.println("response="+response.body()) ;
        /**
         * {"status":"success",
         * 	"data":"{\"title\":\"\",\"sessionName\":\"zentaosid\",
         * 	\"sessionID\":\"7jo894sal1qfo9dhbttd7bums1\",\"rand\":1270,\"pager\":null}","md5":"64d81130e5313304f4cc24023adaaef1"}
         */
        if(obj!=null){
            String status = obj.getString("status");
            if ("success".equals(status)) {
                JSONObject data =  JSONObject.parseObject( (String)obj.getString("data")) ;// obj.getJSONObject("data");
                System.out.println("sessionID="+(String)data.get("sessionID")) ;
                return (String)data.get("sessionID");
            }
        }
        return null;
    }
    @Test
    public void getsessionidTest() throws Exception {
        String sessionId = getsessionId();
        System.out.println(sessionId);
        login(sessionId,"bin_chen","kingdee$2018");
        createTask(sessionId,"128");
    }

    /**
     * 2 授权接口 api/users/access_token   POST请求
     */
    public void login( String sessionId, String account,String password) throws Exception {

        String url =  api_address + "/zentao/user-login.json?zentaosid="+sessionId ;
        System.out.println(url);

        //建立一个NameValuePair数组，用于存储欲传递的参数
        List<BasicNameValuePair> params=new ArrayList<BasicNameValuePair>();
        //添加参数
        params.add(new BasicNameValuePair("account", account));
        params.add(new BasicNameValuePair("password", password));

        //发送请求
        HttpRequest request = HttpUtil.createPost(url) ;
//        request.contentType("application/json");
        request.form("account", account);
        request.form("password", password);
        request.setEncodeUrlParams(true);

        //发送请求
        HttpResponse response = request.execute();
        JSONObject obj =  JSON.parseObject(response.body());
        System.out.println("response="+response.body()) ;

    }




    /**
     *   创建禅道任务  zentao/task-create   POST请求
     */
    public  void createTask(String sessionId,String projectId) throws Exception {
        String url =  api_address + "/zentao/task-create-"+projectId+".json?zentaosid="+sessionId ;
        System.out.println(url);

        //发送请求
        HttpRequest request = HttpUtil.createPost(url) ;
//        request.contentType("application/json");
        request.form("type","devel"); //任务类型 取值范围： | design | devel | com.sxkiler.demo.test | study | discuss | ui | affair | misc*必填
        request.form("name","任务名称1");
        request.form("module","2"); //所属模块
        request.form("assignedTo","admin");//指派给
        request.form("pri","2");//优先级 取值范围：0 | 1 | 2 | 3 | 4
        request.form("desc","任务描述1");


        //发送请求
        HttpResponse response = request.execute();
        JSONObject obj =  JSON.parseObject(response.body());
        System.out.println("response="+response.body()) ;
    }

}
